package com.bootdo.sell.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.sell.domain.CpDO;
import com.bootdo.sell.service.CpService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 套餐关联表
 * 
 * @author mp
 * @email 1992lcg@163.com
 * @date 2020-03-02 09:22:07
 */
 
@Controller
@RequestMapping("/sell/cp")
public class CpController {
	@Autowired
	private CpService cpService;
	
	@GetMapping()
/*	@RequiresPermissions("sell:cp:cp")*/
	String Cp(){
	    return "sell/cp/cp";
	}
	
	@ResponseBody
	@GetMapping("/list")
/*	@RequiresPermissions("sell:cp:cp")*/
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CpDO> cpList = cpService.list(query);
		int total = cpService.count(query);
		PageUtils pageUtils = new PageUtils(cpList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
/*	@RequiresPermissions("sell:cp:add")*/
	String add(){
	    return "sell/cp/add";
	}

	@GetMapping("/edit/{cid}")
/*	@RequiresPermissions("sell:cp:edit")*/
	String edit(@PathVariable("cid") Integer cid,Model model){
		CpDO cp = cpService.get(cid);
		model.addAttribute("cp", cp);
	    return "sell/cp/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
/*	@RequiresPermissions("sell:cp:add")*/
	public R save( CpDO cp){
		if(cpService.save(cp)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
/*	@RequiresPermissions("sell:cp:edit")*/
	public R update( CpDO cp){
		cpService.update(cp);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
/*	@RequiresPermissions("sell:cp:remove")*/
	public R remove( Integer cid){
		if(cpService.remove(cid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
/*	@RequiresPermissions("sell:cp:batchRemove")*/
	public R remove(@RequestParam("ids[]") Integer[] cids){
		cpService.batchRemove(cids);
		return R.ok();
	}
	
}
